package net.jblog.struts.backoffice.form.parameter;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.jblog.business.IConstantes;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class ListParameterForm extends ActionForm {
	private static final long serialVersionUID = -5907457568720371270L;
	
	private List<ParameterForm> parameters = new ArrayList<ParameterForm>();

	public List<ParameterForm> getParameters() {
		return parameters;
	}

	public void setParameters(List<ParameterForm> parameters) {
		this.parameters = parameters;
	}
	
	public void addParameters(ParameterForm parameterForm) {
		this.parameters.add(parameterForm);
	}
	
	private ParameterForm getParameter(String cle) {
		for (ParameterForm parameter : parameters) {
			if(cle.equals(parameter.getCle())) {
				return parameter;
			}
		}
		return null;
	}

	
	public String getParameterBlogTitle() {
		ParameterForm param = getParameter(IConstantes.PARAMETER_TITLE);
		if(param != null) {
			return param.getValeur();
		}
		return null;
	}
	public void setParameterBlogTitle(String valeur) {
		ParameterForm param = getParameter(IConstantes.PARAMETER_TITLE);
		if(param != null) {
			param.setValeur(valeur);
		} else {
			param = new ParameterForm();
			param.setCle(IConstantes.PARAMETER_TITLE);
			param.setValeur(valeur);
			parameters.add(param);
		}
	}
	public String getParameterBlogBaseline() {
		ParameterForm param = getParameter(IConstantes.PARAMETER_BASELINE);
		if(param != null) {
			return param.getValeur();
		}
		return null;
	}
	public void setParameterBlogBaseline(String valeur) {
		ParameterForm param = getParameter(IConstantes.PARAMETER_BASELINE);
		if(param != null) {
			param.setValeur(valeur);
		} else {
			param = new ParameterForm();
			param.setCle(IConstantes.PARAMETER_BASELINE);
			param.setValeur(valeur);
			parameters.add(param);
		}
	}
	public String getParameterBlogDescription() {
		ParameterForm param = getParameter(IConstantes.PARAMETER_DESCRIPTION);
		if(param != null) {
			return param.getValeur();
		}
		return null;
	}
	public void setParameterBlogDescription(String valeur) {
		ParameterForm param = getParameter(IConstantes.PARAMETER_DESCRIPTION);
		if(param != null) {
			param.setValeur(valeur);
		} else {
			param = new ParameterForm();
			param.setCle(IConstantes.PARAMETER_DESCRIPTION);
			param.setValeur(valeur);
			parameters.add(param);
		}
	}

	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		try {
	 		request.setCharacterEncoding("ISO-8859-1");
			this.setParameters(new ArrayList<ParameterForm>());
		} catch (UnsupportedEncodingException e) {
	 		e.printStackTrace();
	 	}
	 	super.reset(mapping, request);
	}
}
